package com.lehuo.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.lehuo.bean.Cart;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CartUtil {
	private static Statement statement = null;

	public static String addCart(String user_id, String product_id,
			String product_name, String product_images, String product_price,
			String num, String addressee, String address, String tel,
			String courier_num, String state, Connection connection) {
		JSONObject jsonObject = new JSONObject();
		String time = StringUtil.getTime();
		System.out.println("time:"+time);
		String addCart_sql = "insert into cart(user_id,product_id,"
				+ "product_name,product_images,product_price,"
				+ "num,addressee,address,tel,courier_num,state,time)values('"
				+ user_id
				+ "','"
				+ product_id
				+ "','"
				+ product_name
				+ "','"
				+ product_images
				+ "','"
				+ product_price
				+ "','"
				+ num
				+ "','"
				+ addressee
				+ "','"
				+ address
				+ "','"
				+ tel
				+ "','"
				+ courier_num + "','" + state +"','" + time + "');";

		try {
			connection = ConnUtil.getConnection();
			statement = (Statement) connection.createStatement();
			System.out.println("register_sql:" + addCart_sql);
			int count = statement.executeUpdate(addCart_sql,
					Statement.RETURN_GENERATED_KEYS);
			ResultSet rs = statement.getGeneratedKeys();
			int key = -1;
			if (rs.next()) {
				key = rs.getInt(1);
				System.out.println(":" + key);
			}
			if (count > 0) {
				jsonObject.put("result", "success");
				jsonObject.put("id", key);
			} else {
				jsonObject.put("result", "error");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	public static String queryCartByUserId(String user_id, Connection connection) {
		JSONArray jsonArray = null;
		ResultSet rs = null;
		String queryCartByUserId_sql = "select * from cart where user_id= '"
				+ user_id + "'";
		List<Cart> carts = new ArrayList<Cart>();

		try {
			connection = ConnUtil.getConnection();
			statement = (Statement) connection.createStatement();
			System.out
					.println("queryCartByUserId_sql:" + queryCartByUserId_sql);
			rs = statement.executeQuery(queryCartByUserId_sql);

			while (rs.next()) {
				Cart cart = new Cart(rs.getInt("id") + "",
						rs.getString("user_id"), rs.getString("product_id"),
						rs.getString("product_name"),
						rs.getString("product_images"),
						rs.getString("product_price"), rs.getString("num"),
						rs.getString("addressee"), rs.getString("address"),
						rs.getString("tel"), rs.getString("courier_num"),
						rs.getString("state"),rs.getString("time"));
				carts.add(cart);
			}
			jsonArray = JSONArray.fromObject(carts);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}

	public static String deleteCart(String id, Connection connection) {
		PreparedStatement pstmt = null;
		JSONObject jsonObject = new JSONObject();
		String deleteRepair_sql = "delete from cart where id=" + id;
		try {
			connection = ConnUtil.getConnection();
			pstmt = connection.prepareStatement(deleteRepair_sql);
			int num = pstmt.executeUpdate();
			System.out.println("deleteRepair_sql:"+deleteRepair_sql);

			if (num > 0) {
				jsonObject.put("result", "success");
			} else {
				jsonObject.put("result", "error");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}
	
	public static String pay(String id, Connection connection) {
		JSONObject jsonObject = new JSONObject();
		PreparedStatement pstmt = null;
		String queren_sql = "UPDATE cart SET state= '1' where id= " + id;
		try {
			connection = ConnUtil.getConnection();
			System.out.println("queren_sql:" + queren_sql);
			pstmt = connection.prepareStatement(queren_sql);
			int num = pstmt.executeUpdate();
			if (num > 0) {
				jsonObject.put("result", "success");
			} else {
				jsonObject.put("result", "error");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}
}
